package com.carrental.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.common.result.Result;
import com.carrental.rental.dto.OrderDTO;
import com.carrental.rental.entity.Order;
import com.carrental.rental.service.OrderService;
import com.carrental.rental.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 订单控制器
 */
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping
    public Long createOrder(@Valid @RequestBody OrderDTO orderDTO) {
        return orderService.createOrder(orderDTO);
    }

    @ApiOperation("根据ID获取订单详情")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public OrderVO getOrderById(@PathVariable Long id) {
        return orderService.getOrderById(id);
    }

    @ApiOperation("根据订单号获取订单详情")
    @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, dataType = "String", paramType = "path")
    @GetMapping("/no/{orderNo}")
    public Result<OrderVO> getOrderByOrderNo(@PathVariable String orderNo) {
        return Result.success(orderService.getOrderByOrderNo(orderNo));
    }

    @ApiOperation("分页查询用户订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数量", defaultValue = "10", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query")
    })
    @GetMapping("/user")
    public Result<Page<OrderVO>> pageUserOrders(@RequestParam(defaultValue = "1") long current,
                                               @RequestParam(defaultValue = "10") long size,
                                               @RequestParam Long userId,
                                               @RequestParam(required = false) Integer status,
                                               @RequestParam(required = false) String startTime,
                                               @RequestParam(required = false) String endTime) {
        Page<Order> page = new Page<>(current, size);
        return Result.success(orderService.pageUserOrders(page, userId, status, startTime, endTime));
    }

    @ApiOperation("分页查询所有订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", defaultValue = "1", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数量", defaultValue = "10", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query")
    })
    @GetMapping
    public Page<OrderVO> pageOrders(@RequestParam(defaultValue = "1") long current,
                                    @RequestParam(defaultValue = "10") long size,
                                    @RequestParam(required = false) String orderNo,
                                    @RequestParam(required = false) Long userId,
                                    @RequestParam(required = false) Integer status,
                                    @RequestParam(required = false) String startTime,
                                    @RequestParam(required = false) String endTime) {
        Page<Order> page = new Page<>(current, size);
        return orderService.pageOrders(page, orderNo, userId, status, startTime, endTime);
    }

    @ApiOperation("支付订单")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/{id}/pay")
    public Result<Boolean> payOrder(@PathVariable Long id) {
        return Result.success(orderService.payOrder(id));
    }

    @ApiOperation("取消订单")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/{id}/cancel")
    public boolean cancelOrder(@PathVariable Long id) {
        return orderService.cancelOrder(id);
    }

    @ApiOperation("确认取车")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/{id}/pickup")
    public boolean confirmPickup(@PathVariable Long id) {
        return orderService.confirmPickup(id);
    }

    @ApiOperation("确认还车")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/{id}/return")
    public boolean confirmReturn(@PathVariable Long id) {
        return orderService.confirmReturn(id);
    }

    @ApiOperation("完成订单")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/{id}/complete")
    public boolean completeOrder(@PathVariable Long id) {
        return orderService.completeOrder(id);
    }
} 